/**
 * Formulario para Abrir um Projeto
 *
 * @this {UIOpenProjectDialog}
 * @version
 */

function UIOpenProjectDialog() {

    /**
     * Variaveis
     */
    var _oOpenProjectDialog = null;
    var _oClassOwner = this;

    /**
     * Inicializa o objeto da classe
     */
    this.init = function() {

        // Dialog Abrir Projeto
        this.initOpenProjectDialog();

    }

    /**
     * Funcoes
     */
    this.initOpenProjectDialog = function() {

        _oOpenProjectDialog = new YAHOO.widget.Dialog("openprojectdialog", {
            constraintoviewport : true,
            fixedcenter : true,
            modal: false
        });
        var handleSubmit = function() {

            //desabilita todas as opcoes do formulario (ao submeter), para envio de todas as informacoes
            _oClassOwner.disableAllOpenProjectModeOptions(false);

            var oLayoutProject = ModelInstance.getLayoutProject();
            oLayoutProject.openProject();
            
        };
        var myButtons = [ {
            text:"Abrir",
            name:"openprojectbutton",
            handler:handleSubmit,
            isDefault:true
        } ];
        _oOpenProjectDialog.cfg.queueProperty("buttons", myButtons);

        var openProjectForm = document.getElementById('openprojectform');
        for (var i=0; i < openProjectForm.openprojectmode.length; i++) {
            openProjectForm.openprojectmode[i].addEventListener("mousedown",this.onChangeOpenProjectMode,false);
        }
    }

    this.showOpenProjectDialog = function() {
        var oLayoutProject = ModelInstance.getLayoutProject();
        if(oLayoutProject.projectIsOpened()) {
            if(confirm(EAppInfoMessages.eSaveAndGainProjectCopy)) {
                oLayoutProject.salvarAsProject();
            }
            oLayoutProject.closeProject();
        }
        _oOpenProjectDialog.render();
        _oOpenProjectDialog.show();

        _oClassOwner.onChangeOpenProjectMode(null);
    }

    this.hideOpenProjectDialog = function() {
        _oOpenProjectDialog.hide();
    }

    this.onChangeOpenProjectMode = function(evt){
        
        var openProjectForm = document.getElementById('openprojectform');

        //desabilita todas as opcoes do formulario
        _oClassOwner.disableAllOpenProjectModeOptions(true);

        if(evt != null) {
            //escolhe o modo de abertura do projeto
            switch(evt.currentTarget.value) {

                case "byfile":
                    openProjectForm.filedir.disabled = false;
                    break;
                case "bydb":
                    openProjectForm.projectname.disabled = false;
                    break;
            }
        }
        else {
            //habilita a opcao de abertura do projeto via arquivo 
            openProjectForm.filedir.disabled = false;
            openProjectForm.projectname.disabled = true;
            openProjectForm.openprojectmode[0].checked = true;
        }
    }

    this.disableAllOpenProjectModeOptions = function(bState) {

        var openprojectform = document.getElementById('openprojectform');
        openprojectform.filedir.disabled = bState;
        openprojectform.projectname.disabled = bState;
    }

    /**
     * Chamada do inicializador da classe
     */
    this.init();
}

